package leetcode.每日一题;

import org.junit.Test;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * @author :songyaozhang
 * @date :Created 2021/8/11 16:16
 * @description :
 */
public class 等差数列划分_II_子序列 {
    @Test
    public void test() {
        Solution solution = new Solution();
        System.out.println(solution.numberOfArithmeticSlices(new int[] {7,7,7,7,7}));
    }

    class Solution {
        public int numberOfArithmeticSlices(int[] nums) {
            int ans = 0;
            int n = nums.length;
            Map<Long, Integer>[] f = new Map[n];
            for (int i = 0; i < n; ++i) {
                f[i] = new HashMap<>();
            }
            for (int i = 0; i < n; ++i) {
                for (int j = 0; j < i; ++j) {
                    long d = 1L * nums[i] - nums[j];
                    int cnt = f[j].getOrDefault(d, 0);
                    ans += cnt;
                    f[i].put(d, f[i].getOrDefault(d, 0) + cnt + 1);
                }
            }
            return ans;
        }
    }
}
